import 'dart:async';
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_screen_recording/flutter_screen_recording.dart';
import 'package:open_file/open_file.dart';
import 'package:quiver/async.dart';
import 'package:path_provider/path_provider.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  bool recording = false;
  int _time = 0;

  requestPermissions() async {
    // await PermissionHandler().requestPermissions([
    //   PermissionGroup.storage,
    //   PermissionGroup.photos,
    //   PermissionGroup.microphone,
    // ]);
  }

  @override
  void initState() {
    super.initState();
    requestPermissions();
    startTimer();
  }

  void startTimer() {
    CountdownTimer countDownTimer = new CountdownTimer(
      new Duration(seconds: 1000),
      new Duration(seconds: 1),
    );

    var sub = countDownTimer.listen(null);
    sub.onData((duration) {
      setState(() => _time++);
    });

    sub.onDone(() {
      print("Done");
      sub.cancel();
    });
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text('Time: $_time\n'),
            !recording
                ? Center(
              child: InkWell(
                child: Text("Record Screen"),
                onTap: () => startScreenRecord(false),
              ),
            )
                : Container(),
            !recording
                ? Center(
              child: InkWell(
                child: Text("Record Screen & audio"),
                onTap: () => startScreenRecord(true),
              ),
            )
                : Center(
              child: InkWell(
                child: Text("Stop Record"),
                onTap: () => stopScreenRecord(),
              ),
            )
          ],
        ),
      ),
    );
  }

  startScreenRecord(bool audio) async {
    bool start = false;
    await Future.delayed(const Duration(milliseconds: 1000));

    if(Platform.isWindows){
      Directory appDocDir = await getApplicationDocumentsDirectory();
      String path = appDocDir.path + "/test_record.mp4";
      print("record path : ${path}");
      start = await FlutterScreenRecording.startWindowsRecordScreen(path, audio);
      if (start) {
        setState(() => recording = !recording);
      }
      return start;
    }

    if (audio) {
      start = await FlutterScreenRecording.startRecordScreenAndAudio("Title" + _time.toString(),  titleNotification:"dsffad", messageNotification: "sdffd");
    } else {
      start = await FlutterScreenRecording.startRecordScreen("Title", titleNotification:"dsffad", messageNotification: "sdffd");
    }

    if (start) {
      setState(() => recording = !recording);
    }

    return start;
  }

  stopScreenRecord() async {
    String path = await FlutterScreenRecording.stopRecordScreen;
    setState(() {
      recording = !recording;
    });
    print("Opening video");
    print(path);
    OpenFile.open(path);
  }
}
